package com.myproject.myblogserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import com.myproject.myblogserver.contant.CommonConstants;
import com.myproject.myblogserver.entity.CommentReply;
import com.myproject.myblogserver.entity.CommentReplyLike;
import com.myproject.myblogserver.service.ICommentReplyService;
import com.myproject.myblogserver.service.ICommentReplyLikeService;
import com.myproject.myblogserver.utils.JwtUtil;

/**
 * <p>
 * 评论回复点赞表 前端控制器
 * </p>
 *
 * @author jiangweike
 * @since 2025-02-25
 */
@Controller
@RequestMapping("/commentReplyLike")
public class CommentReplyLikeController {

    @Autowired
    private ICommentReplyLikeService commentReplyLikeService;

    @Autowired
    private ICommentReplyService commentReplyService;

    /**
     * 点赞评论回复
     */
    @PostMapping("/like/{replyId}")
    public ResponseEntity<?> likeReply(@PathVariable Long replyId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);

            // 验证回复是否存在
            var reply = commentReplyService.lambdaQuery()
                    .eq(CommentReply::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(CommentReply::getId, replyId)
                    .one();

            if (reply == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("评论回复不存在！");
            }

            // 检查是否已经点赞
            var existingLike = commentReplyLikeService.lambdaQuery()
                    .eq(CommentReplyLike::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(CommentReplyLike::getUserId, userId)
                    .eq(CommentReplyLike::getReplyId, replyId)
                    .one();

            if (existingLike != null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("已经点赞过该评论回复！");
            }

            // 创建点赞记录
            var like = new CommentReplyLike();
            like.setUserId(userId);
            like.setReplyId(replyId);
            commentReplyLikeService.save(like);

            return ResponseEntity.ok("点赞成功！");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("点赞失败！");
        }
    }

    /**
     * 取消点赞评论回复
     */
    @PostMapping("/unlike/{replyId}")
    public ResponseEntity<?> unlikeReply(@PathVariable Long replyId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);

            // 查找点赞记录
            var like = commentReplyLikeService.lambdaQuery()
                    .eq(CommentReplyLike::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(CommentReplyLike::getUserId, userId)
                    .eq(CommentReplyLike::getReplyId, replyId)
                    .one();

            if (like == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("未找到点赞记录！");
            }

            // 软删除点赞记录
            commentReplyLikeService.lambdaUpdate()
                    .set(CommentReplyLike::getIsDeleted, CommonConstants.DELETED_STATUS_YES)
                    .eq(CommentReplyLike::getId, like.getId())
                    .update();

            return ResponseEntity.ok("取消点赞成功！");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("取消点赞失败！");
        }
    }
}
